package com.mateng.treasurechest.view.common.adapter

import android.content.Intent
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView

import com.mateng.treasurechest.R
import com.mateng.treasurechest.view.common.bean.MainGridEntity

/**
 * Created by mateng on 2018/1/25.
 */

class MainGridAdapter(private val arrays: List<MainGridEntity>?) : RecyclerView.Adapter<MainGridAdapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_grid_main, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.textView.text = arrays!![position].title
        holder.imageView.setImageDrawable(arrays[position].icon)
        holder.itemView.setOnClickListener({
            holder.imageView.context.startActivity(Intent(holder.imageView.context, arrays[position].className))
        })
    }

    override fun getItemCount(): Int {
        return arrays?.size ?: 0
    }


    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val imageView: ImageView = itemView.findViewById(R.id.iv_icon)
        val textView: TextView = itemView.findViewById(R.id.tv_title)

    }
}
